package com.andyou.rest.web;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.andyou.common.utils.IdGen;
import com.andyou.common.utils.StringUtils;
import com.andyou.rest.entity.AuthEnum;
import com.andyou.rest.entity.Login;
import com.andyou.rest.entity.ResponseBody;
import com.andyou.rest.service.LoginService;

@Path("/api")
@Component
public class LoginController {

	@Autowired
	private LoginService loginService;

	/**
	 * 登录认证接口
	 * @param login （参数不可空）
	 * @return 
	 * @author lgx
	 * Date:2017年4月20日；
	 */
	@POST
	@Path("/login")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public ResponseBody<String> login(Login login) {
		ResponseBody<String> body = new ResponseBody<String>();
		String accessToken = IdGen.uuid();
		String message = loginService.login(login.getAppKey(), login.getAppSecret(),
				login.getLoginName(), login.getPassword(), accessToken);
		// 说明登录成功
		if (StringUtils.isBlank(message)) {
			body.setResultCode(String.valueOf(AuthEnum.success));
			body.setResultMsg("登录成功");
			body.setReturnResult(accessToken);
		}else{
			body.setResultCode(String.valueOf(AuthEnum.failsign));
			body.setResultMsg(message);
		}

		return body;
	}
}
